package com.yz.service.impl;

import com.yz.core.exception.BaseException;
import com.yz.core.exception.UserNotfondException;
import com.yz.dto.UserToken;
import com.yz.entity.SystemUserEntity;
import com.yz.service.SystemUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.util.Password;

/**
 * @className: SysLoginServiceImpl
 * @author: shuishui
 * @date: 2023/01/03 20:15
 */
@Service
public class SysLoginServiceImpl {
    @Autowired
    private SystemUserService systemUserService;
    @Autowired
    private TokenService tokenService;

    public String login(String username, String Password){
        SystemUserEntity systemUser = systemUserService.getByName(username);
        //判断是否查询到密码
        if (systemUser==null){
            throw new UserNotfondException(101,"用户名不存在");
        }
        if (!Password.equals(systemUser.getPassword())){
            throw new BaseException(101, "用户名获密码错误");
        }
        UserToken userToken = tokenService.createToken(systemUser);
        return userToken.getToken();
    }

    public void logout() {
        tokenService.removeToken();
    }
}
